<?php

declare (strict_types = 1);

namespace app\common\service\permission;
use app\common\service\BaseService;
use app\common\repository\permission\MenuRepository;

class MenuService extends BaseService
{
    public function __construct(MenuRepository $repository)
    {
        $this->repository = $repository;
    }

    /**
     * 获取菜单列表
     *
     * @param array $where
     * @return array
     */
    public function getList(array $where = []): array
    {
        $data = $this->repository->getByWhere($where, ['order' => 'desc']);
        return build_tree($data->toArray());
    }

    /**
     * 获取菜单详情信息
     *
     * @param $id
     * @return \app\common\model\BaseModel|array|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getInfo($id): mixed
    {
        return $this->repository->getOne($id);
    }

    /**
     * 创建菜单信息
     *
     * @param array $params
     * @return \app\common\model\BaseModel|array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function create(array $params): \app\common\model\BaseModel|array
    {
        $data = [
            'name' => $this->toPascalCase($params['permission']),
            'title' => $params['title'],
            'permission' => $params['permission'],
            'pid' => $params['pid'],
            'type' => $params['type'],
            'icon' => $params['icon'],
            'route' => $params['route'],
            'redirect' => $params['redirect'],
            'component' => $params['component'],
            'order' => $params['order'],
            'status' => $params['status'],
            'is_show' => $params['is_show'],
            'is_cache' => $params['is_cache'],
            'url' => $params['url'],
            'desc' => $params['desc'],
        ];
        $res = $this->repository->save($data);
        if($params['type'] === 0 && !empty($params['buttons'])) {
            foreach($params['buttons'] as $button) {
                $buttons[] = [
                    'pid' => $res->id,
                    'type' => 1,
                    'title' => $button['title'] ?? '',
                    'permission' => $button['permission'],
                    'i18' => $button['i18'],
                ];
            }
            $this->repository->save($buttons);
        }
        return $res;
    }

    public function toPascalCase($string) {
        return preg_replace_callback('/(?:^|[:_\-\.])([a-z])/', function($matches) {
            return strtoupper($matches[1]);
        }, $string);
    }

    /**
     * 更新菜单信息
     *
     * @param array $params
     * @param $id
     * @return \app\common\model\BaseModel|bool|int
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function update(array $params, $id): \app\common\model\BaseModel|bool|int
    {
        if(empty($id)) {
            throw new \Exception('ID 不能为空');
        }
        $data = [
            'name' => $this->toPascalCase($params['permission']),
            'title' => $params['title'],
            'permission' => $params['permission'],
            'pid' => $params['pid'],
            'type' => $params['type'],
            'icon' => $params['icon'],
            'route' => $params['route'],
            'redirect' => $params['redirect'],
            'component' => $params['component'],
            'order' => $params['order'],
            'status' => $params['status'],
            'is_show' => $params['is_show'],
            'is_cache' => $params['is_cache'],
            'url' => $params['url'],
            'desc' => $params['desc'],
        ];
        // 菜单按钮权限
        if($params['type'] === 0 && !empty($params['buttons'])) {
            foreach($params['buttons'] as $button) {
                $buttons[] = [
                    'id' => $button['id'] ?? null,
                    'pid' => $id,
                    'type' => 1,
                    'title' => $button['title'] ?? '',
                    'permission' => $button['permission'],
                    'i18' => $button['i18'],
                ];
            }
            $this->repository->saveAll($buttons);
        }
        return $this->repository->update($data, $id);
    }

    /**
     * 删除菜单信息
     *
     * @param $id
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function delete($id): bool
    {
        return $this->repository->delete($id);
    }
}